<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /* 粘附模式y 用于查找非法字符、提取词元 */
        let s = 'aaa_aa_a';
        let r1 = /a+/g;
        let r2 = /a+/y;
        let p1 = "";
        let p2 = "";
        let arr1 = [];
        let arr2 = [];
        while (p1 != null) {
            p1 = r1.exec(s);
            arr1.push(p1);
        }
        while (p2 != null) {
            p2 = r2.exec(s);
            arr2.push(p2);
        }
        console.log(arr1, arr2);

        /* dotAll模式s .可以匹配任意字符, 几个.则代表几个字符 */
        let re = /hello...world/s;
        console.log(re.test("hellotheworld"));
        console.log(re.test("hello world"));

        /* 断言 */
        /*
        (?=pattern)先行断言 - 在pattern之前才匹配
    
        (?!pattern)先行否定断言 - 不在pattern之前才匹配
            
        (?<=pattern)后行断言 - 在pattern之后才匹配
            
        (?<!pattern)后行否定断言 - 不在pattern之后才匹配
        */
        let test = /(?<=\$)\d+/.exec('Benjamin Franklin is on the $100 bill');
        console.log(test);

        /* 具名组匹配 ?<xxx> */
        // 字符串替换时，使用$<组名>引用具名组。
        //如果要在正则表达式内部引用某个“具名组匹配”，可以使用\k<组名>的写法。
        const test_detail = /(?<mom>mom) and my (?<dad>dad)/;
        const family = test_detail.exec("My mom and my dad both love me.");
        // + 解构
        let { groups: { mom, dad } } = family;
        console.log(family.groups.mom, family.groups.dad);
        console.log(mom, dad);

        /* String.prototype.matchAll()方法 取出所有匹配，返回一个遍历器 */
        const string = 'test1test2test3';
        const regex = /t(e)(st(\d?))/g;

        for (const match of string.matchAll(regex)) {
            console.log(match);
        }
        // ["test1", "e", "st1", "1", index: 0, input: "test1test2test3"]
        // ["test2", "e", "st2", "2", index: 5, input: "test1test2test3"]
        // ["test3", "e", "st3", "3", index: 10, input: "test1test2test3"]
    </script>
</body>

</html>